package org.jeecg.modules.hotel.belongToSkd.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.hotel.belongToSkd.entity.LwgyStudentBed;
import org.jeecg.modules.hotel.belongToSkd.entity.ThirdStandard;
import org.jeecg.modules.hotel.belongToSkd.entity.ThirdStandardChange;
import org.jeecg.modules.hotel.belongToSkd.service.ThirdStandardChangeService;
import org.jeecg.modules.hotel.belongToSkd.service.ThirdStandardService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;

/**
 * 标准计费信息表专门用来同步标签计费相关的数据,床位价格
 * 它依赖学生学籍信息，每天推送一次到山科大中间库(third_standard表)
 * @author:MJ-BaBa
 * @date: 2021.07.15
 */

@Slf4j
public class ThirdStandardEdcuationJob implements Job {

    @Autowired
    private ThirdStandardService thirdStandardService;
    @Autowired
    private ThirdStandardChangeService thirdStandardChangeService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("============进入定时器=============");
        long start1 = System.currentTimeMillis();
        //所有学生的学历信息，
        List<LwgyStudentBed> studentEduList1 = thirdStandardService.queryStuEdu();
        // 创建一个Map，用于存储学生信息
        Map<String, LwgyStudentBed> studentMap = new HashMap<>();
        for (LwgyStudentBed item : studentEduList1) {
            studentMap.put(item.getStudentName() + item.getStudentNumber(), item);
        }
        //third_standard表学历为空的学生，
        List<ThirdStandard> studentEduList2 = thirdStandardChangeService.querythirdStandard();

        long start2 = System.currentTimeMillis();
        log.info("powerList  耗时：" + (start2 - start1));

        List<ThirdStandard> list = new ArrayList<>();
        //循环每个学生的信息,将信息保存到roomList中
        studentEduList1.stream().forEach(item -> {
            studentEduList2.stream().forEach(item2 -> {
                ThirdStandard thirdStandard = new ThirdStandard();
                if(item.getStudentName().equals(item2.getPersonnelName()) &&
                        item.getStudentNumber().equals(item2.getPersonnelNo())){
                    thirdStandard.setPersonnelNo(item2.getPersonnelNo());
                    thirdStandard.setEducationalBackground(item.getStudentEducation());
                    list.add(thirdStandard);
                }
            });
        });
        // 批量更新学生学历
        // 批量添加学生住宿信息
        int insertLength = list.size();
        int i = 0;
        while(insertLength > 500){
            thirdStandardChangeService.updateBatchByBedIdS(list.subList(i,i + 500));
            i = i + 500;
            insertLength = insertLength - 500;
        }
        if(insertLength > 0){
            thirdStandardChangeService.updateBatchByBedIdS(list.subList(i,i + insertLength));
        }
        //thirdStandardChangeService.updateBatchByBedIdS(list);

    }
}
